/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package treepro;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;


/**
 *
 * @author Ivan
 */
public class NodeObject extends GraphicObject implements Comparable<Object> {

    private ChangeEvent changeEvent = null;
    private int radius= 50;
    private Color backgroundColor = Color.LIGHT_GRAY;
    private double lastX = 0.0;
    private double lastY = 0.0;    
    
    public double unloadTime = 1;
    public double volume = 1;
    public double lapTime = 1;
    public int number = 1; 
    public boolean fu_enable = false;
    public boolean skvoz = false;
    public int fu_value = 1;
    
    public NodeObject(String name, boolean fu_enable, int fu_value, double lapTime, double unloadTime, double volume, double left, double top, int radius, int number, boolean skvoz) {

        this(TreeProApp.main.treePanel, name, left, top, radius);
        this.skvoz = skvoz;
        this.unloadTime = unloadTime;
        this.volume = volume;
        this.lapTime = lapTime;
        this.number = number; 
        this.fu_enable = fu_enable;
        this.fu_value = fu_value;        
    }
    

    public int getRadius() {
        return radius;
    }

    public void setRadius(int radius) {
        this.radius = radius;
        setRect(getLeft(), getTop(), getLeft()+radius, getTop()+radius);        
        //repaint();
    }
    
    
    public Color getBackgroundColor() {
        return backgroundColor;
    }

    public void setBackgroundColor(Color backgroundColor) {
        this.backgroundColor = backgroundColor;
        repaint();
    }
    
    @Override
    public void repaint() {
        changeEvent.fireChangeEvent();
        super.repaint();
    }
    
    public ChangeListener[] getChangeListeners() {
        return changeEvent.getChangeListeners();
    }
    
    public void removechangeListener(ChangeListener listener) {
        changeEvent.removechangeListener(listener);
    }
    
    public void addChangeListener(ChangeListener listener) {
        changeEvent.addChangeListener(listener);
    }
    
    public NodeObject(TreePanel parent, String name, double x, double y, int radius) {
        changeEvent = new ChangeEvent(this);
        this.radius = radius;
        setParent(parent);
        setName(name);
        setRect(x, y, x+radius, y+radius);
    }

    public NodeObject() {
    }
    

    
    @Override
    public void paint(Graphics2D g) {
        if (isSolution())
            g.setStroke(new BasicStroke(3, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 3));
        else
            g.setStroke(new BasicStroke());
        
        if (isSolution())
            g.setColor(getSolutionColor());
        else
            g.setColor(getBackgroundColor());
        
        g.fillRoundRect((int)getLeft(), (int)getTop(), radius, radius, radius, radius);
        if (isSelected()) {
            g.setColor(getSelectedColor());            
        } else {
            g.setColor(getColor());            
        }
        
        g.drawRoundRect((int)getLeft(), (int)getTop(), radius, radius, radius, radius);
        
        g.drawString(getName(), (float)(getLeft()+getWidth()/7), (float)(getTop()+getHeight()/1.8));
        g.drawString("["+number+"]", (float)(getLeft()+getWidth()/4), (float)(getTop()+getHeight()/1.8+14));
        
        
    }

    @Override
    public void onMouseDragged(GraphicObject source, MouseEvent evt) {
        double x=evt.getX()-lastX;
        double y=evt.getY()-lastY;
        setRect(x, y, x+getWidth(), y+getHeight());
    }

    @Override
    public void onMousePressed(GraphicObject source, MouseEvent evt) {
        lastX = evt.getX()-getLeft();
        lastY = evt.getY()-getTop();        
    }

    @Override
    public void onMouseMoved(GraphicObject source, MouseEvent evt) {
        
    }

    @Override
    public void onMouseReleased(GraphicObject source, MouseEvent evt) {
        
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    @Override
    public int compareTo(Object o) {
        if (o instanceof NodeObject)
            return this.number-((NodeObject)o).getNumber();
        else
            return super.compareTo(o);
    }
    
    public int getMaxFu() {
        return (fu_enable)?fu_value:0;
    }
    
}
